import { asyncRoutes, constantRoutes } from '@/router'
import auth from '@/utils/auth'

/**
 * 判断是否拥有权限
 */
function hasPermission(permissionIds, route) {
  if (route.id != null) {
    return permissionIds.indexOf(route.id.toString()) > -1
  } else {
    return true //  如果route没有id属性，就放行权限
  }
}

/**
 * 根据权限过滤路由
 */
export function filterAsyncRoutes(routes, permissionIds) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(permissionIds, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, permissionIds)
      }
      res.push(tmp)
    }
  })
  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit, state }) {
    return new Promise(resolve => {
      let accessedRoutes
      if (auth.getRoleId() == 1) {
        accessedRoutes = asyncRoutes || []
      } else {
        //  如果不是系统管理员，过滤权限
        accessedRoutes = filterAsyncRoutes(asyncRoutes, auth.getPermissionIds())
      }

      commit('SET_ROUTES', accessedRoutes) //  加载左侧菜单的路由数组
      resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

//  同步权限
// syncPermission() {
//   var flag = true
//   var _self = this
//   var routeIds = []
//   var permissionCodes = []

//   //  检查路由操作权限码重复
//   function filterOperations(operations) {
//     operations.forEach((operation) => {
//       if (permissionCodes.indexOf(operation.permissionCode) > -1) {
//         flag = false
//         _self.$message.error('权限操作码【' + operation.permissionCode + '】重复')
//         throw new Error('权限操作码【' + operation.permissionCode + '】重复')
//       } else {
//         permissionCodes.push(operation.permissionCode)
//         if (operation.operations) {
//           filterOperations(operation.operations)
//         }
//       }
//     })
//   }

//   //  设置路由属性
//   function setAsyncRoutes(asyncRoutes) {
//     asyncRoutes.forEach((asyncRoute) => {
//       if (asyncRoute.id == null) {
//         flag = false
//         _self.$message.error('【' + asyncRoute.path + '】路由id不能为空')
//         throw new Error('【' + asyncRoute.path + '】路由id不能为空')
//       } else {
//         if (routeIds.indexOf(asyncRoute.id) > -1) {
//           flag = false
//           _self.$message.error('路由id【' + asyncRoute.id + '】重复')
//           throw new Error('路由id【' + asyncRoute.id + '】重复')
//         } else {
//           routeIds.push(asyncRoute.id)
//           asyncRoute.permissionName = asyncRoute.meta.title
//           if (asyncRoute.children) {
//             setAsyncRoutes(asyncRoute.children)
//           }
//           //  检查路由操作权限码重复
//           if (asyncRoute.operations != null) {
//             filterOperations(asyncRoute.operations)
//           }
//         }
//       }
//     })

//     filterOperations
//   }

//   var asyncRoutes = []
//   this.$store.state.asyncRoutes.forEach((asyncRoute) => {
//     asyncRoutes.push(asyncRoute)
//   })
//   asyncRoutes.splice(asyncRoutes.length - 1, 1)
//   //  设置路由属性
//   setAsyncRoutes(asyncRoutes)

//   if (flag) {
//     this.syncPermissionLoading = true
//     this.$axios({
//       url: '/system/permission/syncPermission',
//       method: 'post',
//       data: asyncRoutes
//     }).then((res) => {
//       if (res.code == '200') {
//         this.$message.success(res.desc)
//         this.permissionTree = []
//         this.getPermissionTree()
//         this.syncPermissionLoading = false
//       }
//     }).catch(() => {})
//   }
// },
